import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView
    },
    {
      path: '/about',
      name: 'about',
      component: () => import('../views/AboutView.vue')
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/login.vue')
    }
  ]
})

router.beforeEach((to, from, next) => {
  if(to.path === '/login') {
    next()
  }else {
    const store = JSON.parse(localStorage.getItem('userinfo'))
    if (store&&store.token) {
      next()
    }else{
      next('/login')
    }
  }
})

export default router
